package com.dodoke.cake.controller;

import com.dodoke.cake.bean.CakeProduct;
import com.dodoke.cake.bean.Classification;
import com.dodoke.cake.service.CatalogService;
import com.dodoke.cake.service.ProductListService;
import com.dodoke.cake.utils.PageUtil;
import com.dodoke.cake.utils.Result;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class IndexPageController {

    @Autowired
    private CatalogService cs;
    @Autowired
    private ProductListService productListService;

    @RequestMapping("/index")
    public String indexPage(){
        return "index";
    }

    /**
     * 首页点击分类展示的该分类的商品列表
     * @return
     */
    @RequestMapping("/list")
    public String list(Model model, int cid ){

        model.addAttribute("typeId", cid);
        return "list";
    }

    /**
     * 首页点击分类展示的该分类的商品列表
     * @return
     */
    @ResponseBody
    @RequestMapping("/getListData")
    public Result getListData(@RequestBody Map<String, String> map) {
        int cid = Integer.parseInt(map.get("cid"));
        System.out.println(cid);
        String currentPage = map.get("nextPage");
        if(currentPage == null) {
            currentPage = "1";
        }
        int count = this.productListService.getCakeNumByCatalogId(cid);
        int allPageNum = PageUtil.getAllPage(count, 10);
        int fromWhichData = PageUtil.getFromWhichData(Integer.valueOf(currentPage), 8);
        List<CakeProduct> list = this.productListService.getProductByCid(cid,fromWhichData,8);
        Result res = new Result();
        Map<String, Object> m = new HashMap<String, Object>();
        m.put("allPage", allPageNum);
        m.put("data", list);
        m.put("count", count);
        res.result = m;
        return res;
    }

    @RequestMapping("/detail")
    public String detail(Model model, int id){
        CakeProduct cakeProduct = productListService.getCakeInfo(id);
        //System.out.println(cakeProduct.getCakeName());
        model.addAttribute("cakeInfo", cakeProduct);
        return "detail";
    }

    /**
     * 首页分类展示
     * @return
     */
    @RequestMapping("/indexClassification")
    @ResponseBody
    public Result indexClassification() {
        List<Classification> catalogs = cs.getAllData();
        System.out.println(catalogs);
        Result res = new Result();
        res.result = catalogs;
        return res;
    }

    /**
     * 获取推荐商品
     * @return
     */
    @RequestMapping("/recommentProduct")
    @ResponseBody
    public Result recommentProduct() {
        List<CakeProduct> list = this.productListService.getRecommentProduct();
        Result res = new Result();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("data", list);
        res.result = map;
        return res;
    }

    /**
     * 获取热卖商品
     * @return
     */
    @RequestMapping("/bestSellProduct")
    @ResponseBody
    public Result bestSellProduct() {
        List<CakeProduct> list = this.productListService.getBestSellProduct();
        Result res = new Result();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("data", list);
        res.result = map;
        return res;
    }
}
